import type { FormInstance } from 'antd/es/form';
import { multiply } from 'lodash';
import moment from 'moment';

// 获取table行高
export const getTableLineHeight = (lineHeight: number) => {
  let heightType: string = 'small';
  if (lineHeight === 2) {
    heightType = 'middle';
  } else if (lineHeight === 3) {
    heightType = 'large';
  }
  return heightType;
};

// 格式化千分位
export function formatThousand(num: number) {
  return num.toString().replace(/\d+/, (n) => {
    // 先提取整数部分
    return n.replace(/(\d)(?=(\d{3})+$)/g, ($1) => {
      // 对整数部分添加分隔符
      return `${$1},`;
    });
  });
}

/**
 * 根据配置处理数字类型展示格式
 */
export function dealNumberShow(
  num: number | any,
  config: {
    displayDigitLen: number;
    percentage: boolean;
    thousandthSymbol: boolean;
    digitLen: number;
  },
) {
  const { displayDigitLen, percentage, thousandthSymbol, digitLen } = config;
  const dig = displayDigitLen === 0 ? 0 : displayDigitLen || (digitLen === 0 ? 0 : digitLen || 10);
  let showVal: any;
  showVal = percentage ? `${getTableDecimal(multiply(num, 100), dig)}%` : getTableDecimal(num, dig);
  if (thousandthSymbol) {
    showVal = formatThousand(showVal);
  }
  return showVal;
}

// 截取到指定小数位，不四舍五入
/**
 *
 * @param {*} data 数据
 * @param {*} dig 显示小数位数
 */
function getTableDecimal(num: any, dig: number) {
  const digLen = +dig + 1;
  let data = String(num);
  const numData = Number(data);
  if (dig === 0) {
    return data.split('.')[0];
  }
  data = numData.toFixed(digLen);
  if (dig === 0) {
    data = String(data).substring(0, data.lastIndexOf('.'));
  } else {
    data = String(data).substring(0, data.lastIndexOf('.') + digLen);
  }
  if (data.lastIndexOf('.') === data.length - 1) {
    // 处理最后一位显示.的情况
    data = String(data).substring(0, data.lastIndexOf('.'));
  }
  return data;
}

const formatArray = <T extends number | string | null | undefined>(a: T | T[]) =>
  Array.isArray(a) ? a : a && a !== 0 ? [a] : [];

// 处理数据显示规则
/**
 *
 * @param params
 * @param isOriginal 是否不需要格式化小数类型
 * @returns
 */
export const formatTableShowData = (
  params: { record: any; cellConfig: any },
  isOriginal?: boolean,
) => {
  const {
    record,
    cellConfig: { valueType, valueTypeMap, thousandthSymbol },
  } = params;
  let text = record?.label || record?.value;
  if (valueType === 8) {
    // w维度
    const { selectedMulti } = valueTypeMap;
    if (valueTypeMap.dimensionDisplay === 0) {
      text = selectedMulti ? formatArray(record?.value) : record?.value;
    } else if (valueTypeMap.dimensionDisplay === 1) {
      text = selectedMulti
        ? formatArray(record?.label || record?.value)
        : record?.label || record?.value;
    } else if (valueTypeMap.dimensionDisplay === 2 && record?.label) {
      // name拼des
      if (selectedMulti) {
        const valueArr = formatArray(record?.value);
        const labelArr = formatArray(record?.label);
        text = valueArr.map((val, i) =>
          val && labelArr[i] ? `${val} - ${labelArr[i]}` : labelArr[i] || val,
        );
      } else {
        text = `${record.value} - ${record?.label}`;
      }
    }
  } else if (valueType === 3) {
    // 值列表
    if (valueTypeMap.smartListDisplay === 0) {
      text = record?.value;
    } else if (valueTypeMap.smartListDisplay === 1) {
      text = record?.label || record?.value;
    } else if (valueTypeMap.smartListDisplay === 2 && record?.label) {
      // name拼des
      text = `${record.value} - ${record?.label}`;
    } else if (record?.label) {
      text = `${record.value} - ${record?.label}`;
    }
  } else if (valueType === 15 && !isOriginal) {
    // 数字
    text =
      typeof text !== 'undefined' && text !== null
        ? dealNumberShow(text, {
            displayDigitLen: valueTypeMap.displayDigitLen,
            percentage: valueTypeMap.percentage,
            thousandthSymbol,
            digitLen: valueTypeMap.digitLen,
          })
        : text;
  } else if (valueType === 11) {
    // 日期类型
    text = text ? moment(text).format(valueTypeMap.instruction) : '';
  }
  return text;
};

export const getScreen = (queryForm: FormInstance) => {
  const queryData = queryForm.getFieldsValue(true)?.list || [];

  return queryData
    .filter((item: any) => item.colAlias)
    .map((item: any) => {
      return {
        colAlias: item.colAlias,
        valueType: item.valueType || 1,
        filterType: item.filterType,
        value: item.valueType === 15 ? (item.value === '' ? null : item.value) : item.value,
        valueList: item.valueList,
      };
    });
};
